<?php

namespace Cyfy\Modules\Users;

use \Cyfy\Message;

class Users extends \Cyfy\Translatable
{
    /**
     * Administration of users.
     * @return string HTML content.
     */
    static public function administration()
    {
        // list of users
        $pager = new \Cyfy\Modules\DibiPager\DibiPager("usersList");
        $pager -> ajaxCall("Users", "users", "#adminUsersList");
        $pager -> sortForm = new \Cyfy\Modules\DibiPager\Forms\SortBy(true);
        $pager -> sortForm -> options = array(
            '[name]' => self :: t(60),
            '[username]' => self :: t(30),
            '[email]' => self :: t(50),
            '[lastLogin]' => self :: t(70)
        );
        
        $output .= $pager -> showSortForm();
    	$output .= "<div id=\"adminUsersList\">" . self :: ajaxUsers() . "</div>";
        $output .= $pager -> pager("SELECT count(*) FROM [:cyfy:Users-users]");
        
        // new user table
        if(\Cyfy\Cyfy ::  permission("Users", "register new users"))
        {
            $output .= "<h2>" . self :: t(620) . "</h2>\n";
            $form = new Forms\Register();
            $form -> setTemp('inAdmin', 'true');
            $output .= $form -> show();
        }
        
		return $output;
	}
    
    
    
    /**
     * Administration of users.
     * @todo Create sorting form.
     * @return string HTML content.
     */
    static public function ajaxUsers()
    {
        if(!\Cyfy\Cyfy ::  permission('Users', 'see user list')){
            \Cyfy\Cyfy ::  noPermission();
            return false;
        }
        
        // users-groups
        $result = \dibi :: query("SELECT [uid], [gid] FROM [:cyfy:Users-users-groups]");
        $usersGroups = array();
		foreach($result as $u){
            $usersGroups[$u["uid"]][] = $u["gid"];
        }
        
        // groups
        $groups = Groups :: getAll(true, true);
        
        // users        
        $pager = new \Cyfy\Modules\DibiPager\DibiPager("usersList");
        $result = $pager -> query("SELECT * FROM [:cyfy:Users-users]");
		
        // Joke start
		if(count($result) == 0)
        {
            // no users found
			return \Cyfy\Message :: get( self :: t(610), \Cyfy\Message :: WARNING);
		}
        // Joke end
       
        $table = new \Cyfy\Modules\Objects\Table();
        $table -> header(array(self :: t(90), self :: t(60), self :: t(30), self :: t(50) , self :: t(70)));
        foreach($result as $u)
        {
            if($u["lastLogin"] != "0000-00-00 00:00:00")
            {
                // get last time login
                $last = \Cyfy\Modules\Locale\Locale :: getTime($u["lastLogin"]);
            }
            else
            {
                // never
                $last = self :: t(420);
            }
            $gs = "";
            // groups
            if(is_array($usersGroups[$u["uid"]])){
                foreach($usersGroups[$u["uid"]] as $g){
                    // if color
                    if($groups[$g]["color"]){
                        $gs .= "<span style=\"font-weight:bold; color: #{$groups[$g]["color"]}\">" . $groups[$g]["short"] . "</span> ";
                    }else{
                        $gs .= $groups[$g]["short"] . " ";
                    }            
                }
            }
            $table -> row(array($gs, $u["name"],$u["username"],$u["email"], $last), "Cyfy.redirectTo('users/{$u["uid"]}');", 1);
        }
        return $table -> show();
    }
    
    /**
     * Removes a user
     * @param int User ID
     */    
    static function deleteUser($uid)
    {
        if(!\Cyfy\Cyfy :: permission("Users", "edit users") && !$uid == Current :: $uid){
            \Cyfy\Cyfy :: noPermission();
            return false;
        }

        \dibi :: query("DELETE FROM [:cyfy:Users-users] WHERE [uid] = %i", $uid);
        \dibi :: query("DELETE FROM [:cyfy:Users-users-groups] WHERE [uid] = %i", $uid);
        
        if(\Cyfy\Modules :: isEnabled("Profiles"))
        {
            \Cyfy\Modules\Profiles\Module :: remove($uid);
        }
        \Cyfy\Message :: set(self::t(1210));
        
        // redirecting
        if( $uid == Current :: $uid){
            unset($_SESSION["userS"]);
            Current :: $uid = 0;
        }
    }
    
}

